<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class HumanResources extends CI_Controller  {

    var $user;
    
    public function index()
    {  
        $this->load->view('welcome_page');
    }
    public function login()
    {
        $this->user = $this->session->userdata('user');        
        if($this->user){
            redirect('/humanresources/profil_korisnika/', 'refresh');            
        } 
        $data['user'] = 'unknown';
        $this->load->view('login',$data);
    }
    public function registrationForm()
    {
        $this->load->view('registracija');
    }

    public function userLogout()
    {
        $this->session->unset_userdata('user');
        $this->index();
    }

    public function userLogin()
    {
        $this->load->model('Users');
        $this->user = $this->Users->findUserByLogin();
        if($this->user){
            $this->session->set_userdata(array('user'=>$this->user));
            redirect('/humanresources/profil_korisnika/', 'refresh');                                             
        }
        else {
            $data['user'] = $this->user;
            $this->load->view('login',$data);
        }        
    }

    public function profil_korisnika() { 
        $this->user = $this->session->userdata('user');

        if($this->user && $this->user[0]->user_level==0)  
        {
            redirect('/humanresources/waitingForAdmin/', 'refresh');
        }    
        $this->auth_user(1);
        $data['user'] = $this->user;  
        $this->load->model('Questionare');        
        $data['questionare']=$this->Questionare->getQuestionare($this->user[0]);        
        $this->load->model('Users');        
        $usr = $this->Users->getUser($this->user[0]->id);
        $data['usr'] = $usr;
        $data['menu'] = $this->load->view('meni',$data,true);
        $this->load->view('profil_korisnika',$data);

    }
    
    private function auth_user($level){  
        $this->user = $this->session->userdata('user');
        $data['user'] = $this->user;             
        if(!$this->user || $this->user[0]->user_level<$level){           
            redirect('/', 'refresh');
        }        
    }

    public function waitingForAdmin(){
        $this->session->sess_destroy();
        $this->load->view('waiting_for_admin');
    }

    public function registracija() {  
        //$this->auth_user(0);
        //$data['user'] = $this->user;
        $this->load->model('Companies');
        $companies = $this->Companies->getAllCompanies();
        $data['companies'] = $companies;   
        $this->load->library('form_validation');
        $this->load->view('registracija',$data);

        
    }

    public function spisak_korisnika($row=0){
        $this->auth_user(0);
        $this->load->model('Users');
        $this->load->library('pagination');
        $config['base_url']=base_url().'humanresources/spisak_korisnika';
        $config['total_rows']=$this->Users->getNumUsers();
        $config['per_page']=10;
        //$config['uri_segment'] = 3;
        //$config['use_page_numbers'] = TRUE;
        $config['full_tag_open'] = '<ul class="uk-pagination">';
        $config['full_tag_close'] = '</ul>';
        $config['first_link'] = '<i class="uk-icon-angle-double-left"></i>';
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['last_link'] = '<i class="uk-icon-angle-double-right"></i>';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';
        $config['prev_link'] = '<i class="uk-icon-angle-left"></i>';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';
        $config['next_link'] = '<i class="uk-icon-angle-right"></i>';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="uk-active"><span>';
        $config['cur_tag_close'] = '</span></li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        $this->pagination->initialize($config);
        $data['user'] = $this->user;
        //$data['users']=$this->Users->getUsers($row);
        $data['users']=$this->Users->getUsersData($row);
        $data['links']=$this->pagination->create_links(); 
        $this->load->model('Questionare');
        $questionare = $this->Questionare->getAllQuestionares();
        $data['questionare'] = $questionare;       
        //$userquestionare = $this->Questionare->getUserQuestionare($user_id);
        //$data['userquestionare'] = $userquestionare;  
        $data['menu'] = $this->load->view('meni',$data,true);
        $this->load->view('spisak_korisnika',$data);
    }

    public function after_register() {

        $this->load->view('after_register');
    }
    public function userRegister()
    {
        $this->load->model('Users');
        $this->load->model('Companies');
        $companies = $this->Companies->getAllCompanies();
        $data['companies'] = $companies; 
        $this->load->library('form_validation');
        $this->form_validation->set_rules('user_name', 'Korisnicko ime', 'required|min_length[3]|max_length[12]|is_unique[users.user_name]');
        $this->form_validation->set_rules('password', 'Lozinka', 'required');
        $this->form_validation->set_rules('first_name', 'Ime', 'required');
        $this->form_validation->set_rules('last_name', 'Prezime', 'required');
        $this->form_validation->set_rules('company_id', 'Kompanija', 'required');
        $this->form_validation->set_rules('sector_id', 'Sektor', 'required');
        $this->form_validation->set_message('required', 'Obavezno polje');
        $this->form_validation->set_message('is_unique', 'Korisnicko ime je zauzeto');
        $this->form_validation->set_message('min_length', 'Min. 3 karaktera');
        $this->form_validation->set_message('max_length', 'Max. 12 karaktera');
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('registracija', $data);
        }
        else
        {
            $this->Users->insertUser();
            redirect('/humanresources/after_register/', 'refresh');
        }

        
         
    }
    public function addUser()
    {
        $this->load->model('Users');

        $this->Users->insertUser();
        redirect('/humanresources/spisak_korisnika/', 'refresh'); 
    }
    public function dodaj_korisnika() {  
        $this->auth_user(2);
        $data['user'] = $this->user; 
        $this->load->model('Companies');
        //$companies = $this->Companies->getAllCompanies();
        //print_r($this->user);
        if($this->user[0]->user_level==2){
            $companies = $this->Companies->getAllCompaniesForUser($this->user[0]->company_id);
        }
        else {
          $companies = $this->Companies->getAllCompanies();  
        }
        //print_r($this->user[0]->company_id);
        $data['companies'] = $companies;  
        $data['menu'] = $this->load->view('meni',$data,true);     
       $this->load->view('dodaj_korisnika',$data);
    }
    public function editUser()
    {
        $id = $_REQUEST["id"];
        $this->load->model('Users');
        $this->Users->updateUser($id);
        redirect('/humanresources/spisak_korisnika/', 'refresh'); 
    }
    public function editSelfUser()
    {
         $id = $_REQUEST["id"];
        $this->load->model('Users');
        $this->load->library('form_validation');
        $this->form_validation->set_rules('password', 'Lozinka', 'required|min_length[3]');
        $this->form_validation->set_message('required', 'Obavezno polje');
        $this->form_validation->set_message('min_length', 'Min. 3 karaktera');
        if ($this->form_validation->run() == FALSE)
        {
            //$this->load->view('izmeni_profil');
            redirect('/humanresources/izmeni_profil/', 'refresh');
        }
        else
        {
            $this->Users->updateUser($id);
            $this->userLogin();
        }
    }
    public function izmeni_korisnika($user_id) { 
        $this->auth_user(0);
        $data['user'] = $this->user;   
        $user_id = $user_id;   
        $this->load->model('Questionare');
        $questionare = $this->Questionare->getUserQuestionare($user_id);
        if(count($questionare)>0)
        $data['questionare'] = $questionare[0]; 
        else {
        $data['questionare']=null;
        }
        $this->load->model('Companies');
        //$companies = $this->Companies->getAllCompanies();
        if($this->user[0]->user_level==2){
            $companies = $this->Companies->getAllCompaniesForUser($this->user[0]->company_id);
        }
        else {
          $companies = $this->Companies->getAllCompanies();  
        }
        $data['companies'] = $companies;
        $this->load->model('Users');
        $usr = $this->Users->getUser($user_id);
        $data['usr'] = $usr;     
        $data['menu'] = $this->load->view('meni',$data,true);   
        $this->load->view('izmeni_korisnika',$data);
    }
    public function izmeni_profil() {
        $this->auth_user(0);
        $this->load->model('Users');
        $user_id = $this->user[0]->id;
        $usr = $this->Users->getUser($user_id);
        $data['usr'] = $usr;
        $data['user'] = $this->user;
        $this->load->model('Companies');
        $companies = $this->Companies->getAllCompanies();
        $data['companies'] = $companies;
        $this->load->model('Questionare');
        //$questionare = $this->Questionare->getAllQuestionares();
        //$data['questionare'] = $questionare;
        $questionare = $this->Questionare->getUserQuestionare($user_id);
        if(count($questionare)>0)
        $data['questionare'] = $questionare[0]; 
        else {
        $data['questionare']=null;
        }
        //$data['questionare'] = $questionare[0];
        $this->load->library('form_validation');
        $data['menu'] = $this->load->view('meni',$data,true);
        $this->load->view('izmeni_profil',$data);
    }
    public function deleteUser($user_id)
    {
        $this->load->model('Users');
        $this->Users->removeUser($user_id);
        redirect('/humanresources/spisak_korisnika/', 'refresh'); 
    }

    public function kompanije() {
        $this->auth_user(3);
        $data['user'] = $this->user;
        $this->load->model('Companies');
        $companies = $this->Companies->getAllCompanies();
        $data['companies'] = $companies;
        $data['menu'] = $this->load->view('meni',$data,true);
        $this->load->view('kompanije',$data);       
    }
    public function opis_posla() {
        $this->auth_user(1);
        $data['user'] = $this->user; 
        $this->load->model('Users');
        $user_id = $this->user[0]->id; 
        $usr = $this->Users->getUser($user_id);
        $data['usr'] = $usr;       
        $this->load->model('Questionare');
        $questionare = $this->Questionare->getQuestionare($this->user[0]);
        if(count($questionare)==0){
        $this->auth_user(0);
        $data['user'] = $this->user;
        $data['menu'] = $this->load->view('meni',$data,true); 
        $this->load->view('opis_posla',$data);    
        } 
        else {
            redirect('/humanresources/finalna_verzija/', 'refresh'); 
        }        
    }
    public function izmene_upitnika() {
        $this->auth_user(1);
        $data['user'] = $this->user;        
        $this->load->model('Questionare');
        $questionare = $this->Questionare->getQuestionare($this->user[0]);
        $data['questionare'] = $questionare[0];
        $data['menu'] = $this->load->view('meni',$data,true);        
        $this->load->view('izmene_upitnika',$data);   
    }
    public function izmeni_upitnik_korisnika($user_id) {        
        $this->auth_user(1);
        $data['user'] = $this->user;       
        $this->load->model('Questionare');
        $this->load->model('Users');
        $usr = $this->Users->getUser($user_id);
        $data['usr'] = $usr;       
        $questionare = $this->Questionare->getUserQuestionare($user_id);
        //print_r($questionare[0]);
        $admin1 = new stdClass();
        $admin1->user_level=3;
        $admin2 = new stdClass();
        $admin2->user_level=2;   

        if($this->user[0]->user_level==2)    
        $data['comments2']= $this->Questionare->getComments($user_id,$admin1);
        if($this->user[0]->user_level==3) 
        $data['comments2']= $this->Questionare->getComments($user_id,$admin2);

        $data['comments']= $this->Questionare->getComments($user_id,$this->user[0]);
        $data['questionare'] = $questionare[0];
        $data['menu'] = $this->load->view('meni',$data,true);       
        $this->load->view('izmeni_upitnik_korisnika',$data);         
    }
    public function finalna_verzija() {
        $this->auth_user(1);
        $data['user'] = $this->user;        
        $this->load->model('Questionare');
        $questionare =  $this->Questionare->getQuestionare($this->user[0]);
        if($questionare){
            $data['questionare'] = $questionare;
            $data['menu'] = $this->load->view('meni',$data,true);
            $this->load->view('finalna_verzija',$data); 
        }
        else {
            redirect('/humanresources/opis_posla/', 'refresh');
        }
        //$questionareold = $this->Questionare->getQuestionareold($this->user[0]);
        /*if(count($questionareold)==0){
        $this->auth_user(0);
        $data['user'] = $this->user;
        $this->load->view('opis_posla',$data);    
        } 
        else {
            $data['questionareold'] = $questionareold[0];
            $this->load->view('finalna_verzija',$data); 
        }   */     
     }

     public function odobriQuestionare(){
        $questionareUser = $this->input->post('questionareUser');
        $this->load->model('Questionare');
        $this->Questionare->verifieQuestionare($questionareUser);
        redirect('/humanresources/spisak_korisnika/', 'refresh');        
     }

     public function finalna_verzija_nova($user_id) {        
        $this->auth_user(0);
        $data['user'] = $this->user;        
        $this->load->model('Questionare');
        $questionare = $this->Questionare->getUserQuestionare($user_id);
       
        $admin1 = new stdClass();
        $admin1->user_level=3;
        $admin2 = new stdClass();
        $admin2->user_level=2;   
        $questionareUser    = $user_id;
        $data['comments']= $this->Questionare->getComments($questionareUser,$admin1);
        $data['comments2']= $this->Questionare->getComments($questionareUser,$admin2);

        if(count($questionare)==0){
        $this->auth_user(0);
        $data['user'] = $this->user;
        $data['menu'] = $this->load->view('meni',$data,true); 
        $this->load->view('opis_posla',$data);    
        } 
        else {
            $data['questionare'] = $questionare[0];
            $data['menu'] = $this->load->view('meni',$data,true);
            $this->load->view('finalna_verzija_korisnika',$data);
        }      
     }
    public function finalna_verzija_korisnika() {
        $this->auth_user(0);
        $data['user'] = $this->user;
        $user_id = $_REQUEST['user_id'];   
        $this->load->model('Questionare');
        $questionare = $this->Questionare->getUserQuestionare($user_id);
        $data['questionare'] = $questionare[0];      
        $data['menu'] = $this->load->view('meni',$data,true); 
        $this->load->view('finalna_verzija_korisnika',$data);          
    }
    public function finalna_verzija_prihvacena() {
        $this->auth_user(0);
        $data['user'] = $this->user;
        $user_id = $_REQUEST['user_id'];   
        $this->load->model('Questionare');
        $questionare = $this->Questionare->getUserQuestionare($user_id);
        $data['questionare'] = $questionare[0]; 
        $data['menu'] = $this->load->view('meni',$data,true);      
        $this->load->view('finalna_verzija_prihvacena',$data);       
    }
    public function finalna_verzija_odobrena() {
        $this->auth_user(0);
        $data['user'] = $this->user;
        $user_id = $_REQUEST['user_id'];   
        $this->load->model('Questionare');
        $questionare = $this->Questionare->getUserQuestionare($user_id);
        $data['questionare'] = $questionare[0];   
        $data['menu'] = $this->load->view('meni',$data,true);    
        $this->load->view('finalna_verzija_odobrena',$data);       
    }
    public function primeri() {
        $this->auth_user(0);
        $data['user'] = $this->user;
        $data['menu'] = $this->load->view('meni',$data,true);
        $this->load->view('primeri',$data);
    }
    public function organogram($company_id) {
        $this->auth_user(0);
        $this->load->model('Users');

        $data['user'] = $this->user;
        $data['users']=$this->Users->getAllUsers();
        $this->load->model('Companies');        
        $data['company'] = $this->Companies->getCompany($company_id);
        $sectors = $this->Companies->getSectors($company_id);
        $this->load->model('Users'); 
        /*foreach ($sectors as $sector) {
            $sector->rm=  $this->Users->getWorkPlaceForSector($sector->id);
        }*/
        $data['sectors'] = $sectors;
        $data['menu'] = $this->load->view('meni',$data,true);
        $organogram_file = $this->load->view('organogram', $data, TRUE);
        if($fp = fopen('./verzije/organogram.html', 'w')) {
            fwrite($fp, $organogram_file);
            fclose($fp);
            }            
               
        $this->load->view('organogram',$data);  

    }

    public function organogramSector($sector_id) {
        $this->auth_user(0);
        $this->load->model('Users');

        $data['user'] = $this->user;
        $data['users']=$this->Users->getAllUsers();
        $this->load->model('Companies');        
        //$data['company'] = $this->Companies->getCompany($company_id);
        $sectors = $this->Companies->getSector($sector_id);
        $this->load->model('Users'); 
        foreach ($sectors as $sector) {
            $sector->rm=  $this->Users->getWorkPlaceForSector($sector->id);
        }
        $data['sectors'] = $sectors;
        $this->load->model('Questionare'); 
        $data['questionares'] = $this->Questionare->getOrganogramQuestionareSectorData($sector->id);        
        $data['menu'] = $this->load->view('meni',$data,true); 
        $organogram_file = $this->load->view('organogram', $data, TRUE);
        if($fp = fopen('./verzije/organogram.html', 'w')) {
            fwrite($fp, $organogram_file);
            fclose($fp);
            }     
                   
        $this->load->view('organogramSector',$data);  
    }
   
    public function proccessQuestionnaire(){
        $this->auth_user(1);
        $this->user = $this->session->userdata('user');
        $this->load->model('Questionare');
        $this->Questionare->saveQuestionare($this->user[0]);
        $this->Questionare->insertRadnomesto($this->user[0]);
        redirect('/humanresources/profil_korisnika/', 'refresh'); 
    }
    public function updateQuestionnaire(){
        $this->auth_user(1);
        $this->user = $this->session->userdata('user');
        $this->load->model('Questionare');
        $this->Questionare->updateQuestionare($this->user[0]);
        $this->Questionare->insertRadnomesto($this->user[0]);
        redirect('/humanresources/finalna_verzija/', 'refresh'); 
    }
    /*public function updateUserQuestionnaire(){ 
        $this->auth_user(1);
        $this->user = $this->session->userdata('user');
        $data['user'] = $this->user;
        //$user_id = $_REQUEST['user_id'];
        $questionareUser = $_REQUEST['questionareUser'];
        
        $this->load->model('Questionare');       
        $admin1 = new stdClass();
        $admin1->user_level=3;
        $admin2 = new stdClass();
        $admin2->user_level=2;
        $this->Questionare->reviewQuestionare($questionareUser);
        $this->Questionare->updateQuestionare($questionareUser,$this->user[0]);
        $data['comments']= $this->Questionare->getComments($questionareUser,$admin1);
        $data['comments2']= $this->Questionare->getComments($questionareUser,$admin2);
        $questionare = $this->Questionare->getUserQuestionare($questionareUser);

        $data['questionare'] = $questionare[0]; 
        $data['menu'] = $this->load->view('meni',$data,true);
        $this->load->view('finalna_verzija_korisnika',$data);
    }*/

    public function updateUserQuestionnaire(){ 
        //process
        $process = $this->input->post('process');
        //print_r($process);
        $this->auth_user(1);
        $this->user = $this->session->userdata('user');
        $data['user'] = $this->user;        
        $questionareUser = $_REQUEST['questionareUser'];
        
        $this->load->model('Questionare');       
        $admin1 = new stdClass();
        $admin1->user_level=3;
        $admin2 = new stdClass();
        $admin2->user_level=2;
        if($this->user[0]->user_level==3 && $process=="Sacuvaj"){
            $this->Questionare->reviewQuestionare($questionareUser);
        }    
        if($this->user[0]->user_level==3 && $process=="Odobravam"){
            $this->Questionare->verifieQuestionare($questionareUser);
        }            
        if($this->user[0]->user_level==2 && ($process=="Prihvatam" || $process=="Prihvatam uz komentar")){
            $this->Questionare->acceptQuestionare($questionareUser);
        }
        $this->Questionare->updateQuestionare($questionareUser,$this->user[0]);
        //$data['comments']= $this->Questionare->getComments($questionareUser,$admin1);
        //$data['comments2']= $this->Questionare->getComments($questionareUser,$admin2);
        //$questionare = $this->Questionare->getUserQuestionare($questionareUser);

        //$data['questionare'] = $questionare[0]; 
        //$data['menu'] = $this->load->view('meni',$data,true);
        //$this->load->view('finalna_verzija_korisnika',$data);
        redirect('/humanresources/spisak_korisnika/', 'refresh');
    }



    public function prihvatiUserQuestionnaire(){
        $this->auth_user(2);
        $this->user = $this->session->userdata('user');
        $data['user'] = $this->user;
        $questionareUser = $_REQUEST['questionareUser'];
        
        $this->load->model('Questionare');       
        $admin1 = new stdClass();
        $admin1->user_level=3;
        $admin2 = new stdClass();
        $admin2->user_level=2;       
        $data['comments']= $this->Questionare->getComments($questionareUser,$admin1);
        $data['comments2']= $this->Questionare->getComments($questionareUser,$admin2);

        $questionareUser = $_REQUEST['questionareUser'];
        $this->load->model('Questionare');
        //$this->Questionare-->updateQuestionare($questionareUser,$this->user[0]);

        $this->Questionare->acceptQuestionare($questionareUser);
        $questionare = $this->Questionare->getUserQuestionare($questionareUser);
        $data['questionare'] = $questionare[0]; 
        $data['menu'] = $this->load->view('meni',$data,true);
        $this->load->view('finalna_verzija_prihvacena',$data);
    }
    public function odobriUserQuestionnaire(){
        $this->user = $this->session->userdata('user');
        $data['user'] = $this->user;
        $questionareUser = $_REQUEST['questionareUser'];
        
        $this->load->model('Questionare');       
        $admin1 = new stdClass();
        $admin1->user_level=3;
        $admin2 = new stdClass();
        $admin2->user_level=2;       
        $data['comments']= $this->Questionare->getComments($questionareUser,$admin1);
        $data['comments2']= $this->Questionare->getComments($questionareUser,$admin2);

        $questionareUser = $_REQUEST['questionareUser'];
        $this->load->model('Questionare');
        //$this->Questionare->updateUserQuestionare($questionareUser);
        $this->Questionare->verifieQuestionare($questionareUser);
        $questionare = $this->Questionare->getUserQuestionare($questionareUser);
        $data['questionare'] = $questionare[0];    
        $data['menu'] = $this->load->view('meni',$data,true);  
        $this->load->view('finalna_verzija_odobrena',$data);
    }

    public function proccessComment(){
        $this->user = $this->session->userdata('user');
        $this->load->model('Comments');
        $this->Comments->saveComment($this->user[0]);
    }
    public function addCompany(){
        $this->user = $this->session->userdata('user');
        $data['user'] = $this->user;
        $this->load->model('Companies');
        $this->Companies->insertCompany();
        $companies = $this->Companies->getAllCompanies();
        $data['companies'] = $companies;
        $data['menu'] = $this->load->view('meni',$data,true);
        $this->load->view('kompanije',$data);
    }
    public function deleteCompany()
    {
        $this->load->model('Companies');
        $this->Companies->removeCompany();
        redirect('/humanresources/kompanije/', 'refresh'); 
    }
    public function deleteSektorColumns($sector_id)
    {        
        //$sector_id = $_REQUEST["sektor_id"];
        $this->load->model('Companies');
        $this->Companies->removeSektor($sector_id);
        redirect('/humanresources/kompanije/', 'refresh'); 
    }
    public  function addSektorColumns()
    {
        $id = $_REQUEST["id"];
        $this->load->model('Companies');
        $this->Companies->addSektor($id);
        redirect('/humanresources/kompanije/', 'refresh');
    }
    public  function ajaxSectors()
    {
        $company_id = $_POST['firma'];
        $user_level = $_POST['user_level'];
        $sector_id = $_POST['sector_id'];
        $this->load->model('Companies');
        if($user_level==2){
             $sectors = $this->Companies->getLimitedSectors($company_id,$sector_id);
        }
        else{
             $sectors = $this->Companies->getSectors($company_id);        
        }
        $data['sectors'] = $sectors;
        $this->load->view('ajax_sectors',$data);
    }

    

    
}